
package HibernateLayer;

import POJOs.SCRUDBasic;
import POJOs.administrators;
import POJOs.client;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author fabiofurlan
 */
public class VUserBasicLoginServices extends VCRUDBasicServices<client> {

    
    @Override
    protected Class<client> getTypeClass() {
        return client.class;
    }

public SCRUDBasic isClient(String username, String password, Session session){

    try {

        Transaction trx=session.beginTransaction();
        System.out.println("VUserBasicLoginServices | isClient | Session-Transaction init.......DONE");

        
        List<client> clientList=   session.createCriteria(type).
                                        add(Restrictions.eq("username", username)).
                                        add(Restrictions.eq("password", password)).
                                        list();

        trx.commit();

        System.out.println("VUserBasicLoginServices | isClient | The system is looking for username inside client table.......DONE");
        System.out.println("VUserBasicLoginServices | isClient | Inside client table the system retrieve......."+clientList.size()+" items");

        if(clientList!=null && !clientList.isEmpty()){
            System.out.println("VUserBasicLoginServices | isClient | The system return the retrieved client item !");
            return clientList.get(0);
        }
        //We check if the user is in the worker(admin) tables
        else{
            trx=session.beginTransaction();

            List<administrators> workerList= session.createCriteria(administrators.class).
                                            add(Restrictions.eq("username", username)).
                                            add(Restrictions.eq("password", password)).
                                            list();

            trx.commit();

            System.out.println("VUserBasicLoginServices | isClient | The system is looking for username inside worker/admin table.......DONE");
            System.out.println("VUserBasicLoginServices | isClient | Inside worker/admin table the system retrieve......."+clientList.size()+" items");
        
            if(workerList!=null && !workerList.isEmpty()){
                System.out.println("VUserBasicLoginServices | isClient | The system return the retrieved admin item !");
                return workerList.get(0);
            }
            else{
                System.out.println("VUserBasicLoginServices | isClient | Wrong username/password !");
                return null;
            }

        }
           
    } catch (Exception e) {
         e.printStackTrace();
         return null;
    }

}

}
